package com.dina.saas.peccancy.service;

import org.apache.log4j.Logger;

import com.dina.saas.peccancy.domain.RequestPeccMsg;
import com.dina.saas.peccancy.domain.ResponsePeccMsg;
import com.dina.saas.peccancy.util.Constants;
import com.dina.saas.peccancy.util.Location;
import com.dina.saas.peccancy.util.MessageUtil;
import com.dina.saas.peccancy.util.Task;
import com.dina.saas.peccancy.util.TaskFactory;

public class DownLoadService {

	private Location location;
	private Task task;
	private Logger logger = Logger.getLogger(DownLoadService.class);

	public ResponsePeccMsg getPeccMsg(RequestPeccMsg requestMsg) {
		// 根据省市得到地方
		getLocation(requestMsg);
		if (null == location) {
			ResponsePeccMsg peccMsg = new ResponsePeccMsg("listPecc", 1, "");
			peccMsg.setResultNote(Constants.LOCATION_NOT_FIND);
			return peccMsg;
		}
		this.task = TaskFactory.getTask(location);
		task.setRequestMsg(requestMsg);
		task.setUrl(MessageUtil.get(location.getName()));
		task.setPicUrl(MessageUtil.get(location.getPic()));
		return task.execute();
	}

	public void getLocation(RequestPeccMsg requestMsg) {
		String tag = requestMsg.getParams().getBrandId().substring(0, 2);
		String locationName = MessageUtil.get(tag);
		if (locationName != null) {
			this.location = new Location(locationName);
		} else {
			logger.error("cant get the location of "
					+ requestMsg.getParams().getBrandId());
		}
		// to do
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public void setTask(Task task) {
		this.task = task;
	}

}
